<template>
  <header class="topbar">
    <div class="topbar-left">
      <div class="breadcrumb">
        <template v-for="(item, index) in breadcrumbItems" :key="index">
          <span 
            class="breadcrumb-item"
            :class="{
              'breadcrumb-home': item.isHome,
              'breadcrumb-parent': item.isParent,
              'breadcrumb-current': item.isCurrent,
              'breadcrumb-clickable': item.path && !item.isCurrent
            }"
            @click="handleBreadcrumbClick(item)"
          >
            <span v-if="item.icon" class="breadcrumb-icon">{{ item.icon }}</span>
            <span class="breadcrumb-text">{{ item.title }}</span>
          </span>
          <span v-if="index < breadcrumbItems.length - 1" class="breadcrumb-separator">/</span>
        </template>
      </div>
    </div>
    
    
    <div class="topbar-right">
      <div class="topbar-actions">
        <!-- 用户菜单 -->
        <div class="user-menu" ref="userMenuRef">
          <div class="user-info" @click="toggleUserMenu">
            <div class="user-avatar">
              <span class="avatar-text">'👤' </span>
            </div>
            <div class="user-details">
              <div class="user-name">{{ user?.userName || '用户' }}</div>
              <div class="user-role">{{ getRoleDisplayName(user?.role) }}</div>
            </div>
            <span class="dropdown-arrow" :class="{ 'rotated': showUserMenu }">▼</span>
          </div>
          
          <!-- 用户下拉菜单 -->
          <div v-if="showUserMenu" class="user-dropdown">
            <div class="dropdown-menu">
              <button class="dropdown-item logout-item" @click="handleLogout">
                <span class="dropdown-icon">🚪</span>
                <span class="dropdown-text">退出登录</span>
              </button>
            </div>
          </div>

          <!-- 退出登录确认弹窗 -->
          <div v-if="showLogoutConfirm" class="logout-confirm-popup">
            <div class="popup-content">
              <div class="popup-text">确定要退出登录吗？</div>
              <div class="popup-actions">
                <button class="popup-btn cancel" @click="cancelLogout">取消</button>
                <button class="popup-btn confirm" @click="confirmLogout">确定</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { useBreadcrumb } from '../composables/useBreadcrumb'

const router = useRouter()
const authStore = useAuthStore()
const { breadcrumbItems, handleBreadcrumbClick } = useBreadcrumb()

// 用户菜单状态
const showUserMenu = ref(false)
const userMenuRef = ref(null)
const showLogoutConfirm = ref(false)

// 获取用户信息
const user = computed(() => {
  console.log('Topbar 获取用户信息:', authStore.user)
  return authStore.user
})

// 获取角色显示名称
const getRoleDisplayName = (role) => {
  const roleNames = {
    admin: '系统管理员',
    superadmin: '超级管理员',
    manager: '宿舍管理员',
    dormitory: '宿管员',
    student: '学生',
    repair: '维修人员',
    maintainer: '维护员'
  }
  return roleNames[role] || '未知角色'
}

// 切换用户菜单
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 点击外部关闭菜单
const handleClickOutside = (event) => {
  if (userMenuRef.value && !userMenuRef.value.contains(event.target)) {
    showUserMenu.value = false
  }
}

// 登出处理
const handleLogout = () => {
  showLogoutConfirm.value = true
  showUserMenu.value = false
}

// 确认登出
const confirmLogout = () => {
  authStore.logout()
  router.push('/login')
  showLogoutConfirm.value = false
}

// 取消登出
const cancelLogout = () => {
  showLogoutConfirm.value = false
}


// 生命周期
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.topbar {
  height: 72px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2rem;
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
  flex-shrink: 0;
}

.topbar-left {
  flex: 1;
  display: flex;
  align-items: center;
}

.topbar-center {
  flex: 2;
  display: flex;
  justify-content: center;
  max-width: 500px;
  margin: 0 2rem;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.breadcrumb-item {
  color: var(--text-muted);
  transition: all 0.2s ease-in-out;
  border-radius: var(--radius);
  padding: 0.25rem 0.5rem;
  margin: 0 0.125rem;
}

.breadcrumb-item:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.breadcrumb-separator {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin: 0 0.25rem;
}

.breadcrumb-current {
  color: var(--text-primary);
  font-weight: 600;
  background: var(--bg-secondary);
}

.breadcrumb-clickable {
  cursor: pointer;
}

.breadcrumb-clickable:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
  transform: translateY(-1px);
}

.breadcrumb-icon {
  margin-right: 0.375rem;
  font-size: 0.875rem;
}

.breadcrumb-text {
  font-size: 0.875rem;
}


.topbar-right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.action-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

.action-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
  transform: translateY(-1px);
}

.action-icon {
  font-size: 1.125rem;
}

.notification-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  background: var(--error);
  color: white;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.125rem 0.375rem;
  border-radius: 9999px;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

.user-menu {
  margin-left: 0.5rem;
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

.user-info:hover {
  background: var(--bg-secondary);
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 0.875rem;
  box-shadow: var(--shadow);
}

.avatar-text {
  font-size: 1rem;
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.user-name {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.875rem;
  line-height: 1.2;
}

.user-role {
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.2;
}

.dropdown-arrow {
  font-size: 0.75rem;
  color: var(--text-muted);
  transition: transform 0.2s ease-in-out;
}

.user-info:hover .dropdown-arrow {
  transform: rotate(180deg);
}

.rotated {
  transform: rotate(180deg);
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  min-width: 240px;
  z-index: 1000;
  animation: slideDown 0.2s ease-out;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}


.dropdown-menu {
  padding: 0.5rem 0;
}

.dropdown-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  text-align: left;
  font-size: 0.875rem;
}

.dropdown-item:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.dropdown-item.logout-item {
  color: var(--error);
}

.dropdown-item.logout-item:hover {
  background: rgba(239, 68, 68, 0.1);
  color: var(--error);
}

.dropdown-icon {
  font-size: 1rem;
  width: 20px;
  text-align: center;
}

.dropdown-text {
  font-size: 0.875rem;
  font-weight: 500;
}

/* 退出登录确认弹窗样式 */
.logout-confirm-popup {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  z-index: 1000;
}

.popup-content {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  min-width: 200px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.popup-text {
  color: var(--text-primary);
  font-size: 0.875rem;
  margin-bottom: 12px;
  text-align: center;
}

.popup-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.popup-btn {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  min-width: 60px;
}

.popup-btn.cancel {
  background: #ef4444;
  color: white;
  border: none;
}

.popup-btn.cancel:hover {
  background: #dc2626;
  color: white;
}

.popup-btn.confirm {
  background: #10b981;
  color: white;
}

.popup-btn.confirm:hover {
  background: #059669;
}

</style>
